package com.itheima.tliasmanagement.controller;

import com.itheima.tliasmanagement.dto.AddClazzDto;
import com.itheima.tliasmanagement.dto.ClassListDto;
import com.itheima.tliasmanagement.dto.UpdateClazzDto;
import com.itheima.tliasmanagement.entity.Clazz;
import com.itheima.tliasmanagement.service.ClazzService;
import com.itheima.tliasmanagement.vo.ClazzListVo;
import com.itheima.tliasmanagement.vo.PageBean;
import com.itheima.tliasmanagement.vo.Result2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    @GetMapping
    public Result2<PageBean<ClazzListVo>> pageQuery(ClassListDto dto){
        PageBean<ClazzListVo> page = clazzService.pageQuery(dto);
        return Result2.ok(page);
    }

    @PostMapping
    public Result2 add(@RequestBody AddClazzDto dto){
        clazzService.add(dto);
        return Result2.ok();
    }

    @GetMapping("{id}")
    public Result2<Clazz> getById(@PathVariable("id")Integer id){
        Clazz vo = clazzService.getById(id);
        return Result2.ok(vo);
    }

    @PutMapping
    public Result2 updateClazz(@RequestBody UpdateClazzDto dto){
        clazzService.updateClazz(dto);
        return Result2.ok();
    }

    @DeleteMapping("{id}")
    public Result2 deleteById(@PathVariable("id")Integer id){
        clazzService.deleteById(id);
        return Result2.ok();
    }

    @GetMapping("list")
    public Result2<List<Clazz>> all(){
        List<Clazz> list = clazzService.all();
        return Result2.ok(list);
    }
}
